<?php
namespace app\admin\controller;

use app\admin\logic\MenuLogic;
use app\admin\model\UserModel;
use app\admin\model\NewsModel;
use app\admin\model\CommentModel;
use app\admin\model\MessageModel;
use app\admin\model\NoteModel;
use app\admin\model\MemberModel;

class Index extends Base
{

    public function index()
    {
        // 获取菜单
        $menu     = new MenuLogic();
        $menulist = $menu->get_menu_arr();

        // 获取用户信息
        $user   = new UserModel();
        $userid = session('user.user_id');
        $info   = $user->get_info(['user_id' => $userid]);
        unset($info['password']);
        unset($info['salt']);

        // 搜索列表
        $list = config("logic.index_select");

        $data = [
            'userinfo' => $info,
            'menulist' => $menulist,
            'slist'    => $list,
        ];
        
        // 搜索
        $module = input("param.module/d");
        if (!empty($module)) {
            $url  = $list[$module]['url'];
            if (!empty($url)) {
                $data['tourl'] = $url;
            }
        }

        return $this->fetch("/index", $data);
    }

    public function main()
    {
        $user    = new UserModel();
        $news    = new NewsModel();
        $comment = new CommentModel();
        $message = new MessageModel();
        $note    = new NoteModel();
        $member  = new MemberModel();

        // 最新文章
        $where = ['status' => 1];
        $order = "create_time desc";
        $start = 0;
        $size  = 8;
        $list  = $news->get_list_page($where, $order, $fields = "*", $start, $size);


        // 新增评论
        $count_comment = $comment->get_count(['read' => -1]);
        // 新增留言
        $count_message = $message->get_count(['type' => 1,'scan' => -1]);
        // 记事本
        $count_note    = $note->get_count("1=1");
        // 管理员
        $count_user    = $user->get_count("1=1");
        // 用户总数
        $count_member  = $member->get_count("1=1");
        // 文章总数
        $count_news    = $news->get_count("1=1");
        
        $data = [
            'newslist'      => $list,
            'count_comment' => $count_comment,
            'count_message' => $count_message,
            'count_note'    => $count_note,
            'count_user'    => $count_user,
            'count_member'  => $count_member,
            'count_news'    => $count_news,
        ];

        return $this->fetch("main", $data);
    }

    public function select()
    {
        $module = input("param.module/d");

        if (empty($module)) {
            $this->error("发生错误");exit;
        }

        $list = config("logic.index_select");
        $url  = $list[$module]['url'];
        if (empty($url)) {
            $this->error("发生错误");exit;
        }

        $this->redirect($url);
    }
}
